// pages/index/details.js
var util = require('../../utils/util.js');
var amap = require('../../utils/amap-wx.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    shopData: '',
    latitude: '', //纬度
    longitude: '', //经度
    isHidden: true,
    hasPay:false    //是否能支付
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '吃喝玩乐推荐'
    })
    var that = this;
    that.setData({
      shopId: options.id
    });
    that.getShopDetail();
  },

  /**
   * 立即购买
   */
  payBtnClick: function() {
    var that = this;
    //是否登录过  1是  0否
    if (util.user) {
      that.userLogin();
    } else {
      //未登录
      app.login(function(user) {
        that.userLogin();
      }, function() {
        //跳转登录
        that.navigation(false);
      });
    }
  },

  /**
   * 页面跳转逻辑
   */
  userLogin: function() {
    var that = this;
    //是否绑手机  
    if (util.user.phone.length > 0) {
      //支付
      that.payment();
    } else {
      //未绑手机
      that.navigation(false);
      
      /** 
      if (util.user.ishome) {
        //跳转首页,此处不需要

      } else {
        //跳转登录
        that.navigation(false);
      }
       */
      
    }
  },

  /**
   * 页面跳转
   */
  navigation: function(isRet) {
    //isRet  false:登录   true:首页
    if (isRet) {
      wx.switchTab({
        url: 'index'
      })
    } else {
      wx.navigateTo({
        url: 'login',
      })
    }
  },

  /**
   * 支付
   */
  payment: function() {
    var that = this;
    var gid = that.data.shopData.gid;
    var nowprice = that.data.shopData.nowprice;
    var sid = that.data.shopData.sid;
    var openid = util.user.openid;
    var params = {
      gid: gid,
      nowprice: nowprice,
      sid: sid,
      openid: openid
    }
    util.POST(util.URL.goods_pay, params, function(sData) {
      var info = sData.info
      var appId = info.appId
      var nonceStr = info.nonceStr
      var packages = info.package
      var paySign = info.paySign
      var signType = info.signType
      var timeStamp = info.timeStamp
      var orderid = info.orderid

      wx.requestPayment({
        'timeStamp': timeStamp,
        'nonceStr': nonceStr,
        'package': packages,
        'signType': signType,
        'paySign': paySign,
        'success': function(res) {
          wx.navigateTo({
            url: 'orderdetails?id=' + orderid,
          })
        },
        'fail': function(res) {
          wx.showToast({
            title: res.errMsg,
            icon: 'none'
          })
        }
      })
    }, function(eData) {
      wx.showToast({
        title: res.retinfo,
        icon: 'none'
      })
    })
  },
  /**
   * 联系门店 打电话
   */
  bind_tel: function() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shopData.phone
    })
  },

  /**
   * 到这去
   */
  tapMap: function() {
    var that = this
    wx.showLoading({
      title: '正在定位...'
    });
    //地理位置==>地理坐标
    var url_address = util.GaoName.shop_coordinate
    wx.request({
      url: url_address,
      data: {
        key: util.GaoName.GaodeKey,
        address: that.data.shopData.address,
        output: 'JSON',
      },
      method: 'GET',
      success: function(data) {
        wx.hideLoading();
        var location_1 = data.data.geocodes[0].location.split(',');
        that.setData({
          longitude: location_1[0],
          latitude: location_1[1],
        });
        that.openShopLocation();
      },
      fail: function(e) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '位置获取失败',
          confirmColor: '#5599ff',
          success: function(res) {
            if (res.confirm) {} else if (res.cancel) {}
          }
        })
      }
    })
  },
  //当前店铺位置信息
  openShopLocation: function() {
    var that = this;
    wx.openLocation({
      longitude: parseFloat(that.data.longitude),
      latitude: parseFloat(that.data.latitude),
      name: that.data.shopData.storename,
      address: that.data.shopData.address
    })
  },

  /**
   * 商品详情
   */
  getShopDetail: function() {
    var that = this;
    wx.showLoading({
      title: '加载中'
    });
    var url = util.URL.goods_detail + '?id=' + that.data.shopId;
    util.GET(url, function(sData) {
      wx.hideLoading();
      that.setData({
        shopData: sData.info,
        isHidden: false
      })
      var intro = that.data.shopData.intro;
      WxParse.wxParse('intro', 'html', intro, that, 5);
    }, function(eData) {
      wx.hideLoading();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    //是否跳转首页 true是  false否
    var userInfo = util.user;
    userInfo.ishome = false;
    util.user = userInfo;
  },
})